
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>

class Solution {
 public:
  std::string reverseStr(std::string s, int k)
  {
    auto r = s.begin();

    while (r != s.end())
    {
      auto sz = std::distance(r, s.end());
      if (sz > 2 * k)
      {
        std::reverse(r, r + k);
        r = r + 2 * k;
      } else if (sz > k && sz <= 2 * k)
      {
        std::reverse(r, r + k);
        r = s.end();
      } else if (sz <= k)
      {
        std::reverse(r, s.end());
        r = s.end();
      }
    }
    return s;
  }
};
